home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / EWDLL011.ZIP / EWDLL.CPP < prev    next >
C/C++ Source or Header  |  1994-02-17  |  11KB  |  313 lines

  1. /*---------------------------------------------------------------------------*/
  2. /* EWDLL.CPP - Borland C++ 4.0 - 256 Color BMP DLL for WinHelp 3.1           */
  3. /* Copyright (C) 1994 by R&R Engineering - All Rights Reserved               */
  4. /*                                                                           */
  5. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  6. /*                                                                           */
  7. /*  This library is free software; you can redistribute it and/or modify it  */
  8. /*  under the terms of the GNU Library General Public License as published   */
  9. /*  by the Free Software Foundation; either version 2 of the License, or     */
  10. /*  (at your option) any later version.                                      */
  11. /*                                                                           */
  12. /*  This library is distributed in the hope that it will be useful, but      */
  13. /*  WITHOUT ANY WARRANTY; without even the implied warranty of               */
  14. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        */
  15. /*  Library General Public License for more details.                         */
  16. /*                                                                           */
  17. /*  You should have received a copy of the GNU Library General Public        */
  18. /*  License along with this library; if not, write to the Free Software      */
  19. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  20. /*                                                                           */
  21. /*---------------------------------------------------------------------------*/
  22.  
  23. #define STRICT
  24.  
  25. #include <stdlib.h>
  26. #include <windowsx.h>
  27. #include "ewdll.h"
  28.  
  29. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  30.  
  31. // Define the default window procedure
  32. #define EWBMP_DefProc DefWindowProc
  33.  
  34. // Window class name
  35. static char ClassName[] = "ewBitmap";
  36.  
  37. // Define a structure for storing bitmap information for a window
  38. typedef struct {
  39.    HBITMAP hbmp;
  40.    HPALETTE hpal;
  41.    int width, height;
  42. } EWBMPINFO;
  43.  
  44. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  45.  
  46. LONG _lsize( HFILE hFile ) {
  47.  
  48.    /*---------------------------------------------------------------------*/
  49.    /*  Return the length (in bytes) of an open file.                      */
  50.    /*---------------------------------------------------------------------*/
  51.  
  52.    LONG p = _llseek( hFile, 0, 1 );  // Save position in the file
  53.    LONG s = _llseek( hFile, 0, 2 );  // Get the length
  54.    _llseek( hFile, p, 0 );           // Restore original position
  55.    return s;                         // Return length
  56. }
  57.  
  58. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  59.  
  60. HBITMAP LoadDIBitmap( char *filespec, HPALETTE *palette ) {
  61.  
  62.    /*---------------------------------------------------------------------*/
  63.    /*  Load a Windows 3.x device independent BMP file.  The return value  */
  64.    /*  is a handle to the logical bitmap if successful, or zero if an     */
  65.    /*  error occurred.  For all besides 24-bit bitmaps, the palette       */
  66.    /*  parameter is a handle to the palette required to properly display  */
  67.    /*  the bitmap.  For 24-bit bitmaps palette is set to zero.            */
  68.    /*---------------------------------------------------------------------*/
  69.  
  70.    BITMAPFILEHEADER bmfh;
  71.    BITMAPINFO *bmi;
  72.    BITMAPINFOHEADER *bmih;
  73.    LOGPALETTE *lp;
  74.    unsigned colors;
  75.    HBITMAP ldib;
  76.  
  77.    // Open the bitmap file for read-only access
  78.    HFILE hFile = _lopen( filespec, READ );
  79.    if (hFile == HFILE_ERROR) return 0;
  80.  
  81.    // Read the file header
  82.    if (_lread( hFile, &bmfh, sizeof( bmfh ) ) != sizeof( bmfh )) {
  83.       _lclose( hFile );
  84.       return 0;
  85.    }
  86.  
  87.    // Check for the Windows 3 bitmap file signature
  88.    if (bmfh.bfType != 0x4D42) {
  89.       _lclose( hFile );
  90.       return 0;
  91.    }
  92.  
  93.    // Allocate a buffer to contain the bitmap data
  94.    LONG bufsize = _lsize( hFile ) - sizeof( bmfh );
  95.    HGLOBAL buffer = GlobalAlloc( GMEM_MOVEABLE, bufsize );
  96.    if (buffer == 0) {
  97.       _lclose( hFile );
  98.       return 0;
  99.    }
  100.  
  101.    // Lock the buffer and read the bitmap data
  102.    bmi = (BITMAPINFO*)GlobalLock( buffer );
  103.    if (_hread( hFile, bmi, bufsize ) == bufsize) {
  104.  
  105.       // Check validity of BITMAPINFOHEADER structure
  106.       bmih = &bmi->bmiHeader;
  107.       if (bmih->biSize == sizeof( BITMAPINFOHEADER )) {
  108.  
  109.          // If this is not a 24-bit bitmap then create a palette
  110.          if (bmih->biPlanes * bmih->biBitCount != 24) {
  111.  
  112.             // Determine the number of colors used
  113.             if (bmih->biClrUsed != 0)
  114.                colors = (unsigned)bmih->biClrUsed;
  115.             else
  116.                colors = 1 << (bmih->biPlanes * bmih->biBitCount);
  117.  
  118.             // Allocate memory for a logical palette
  119.             lp = (LOGPALETTE*)malloc( sizeof( LOGPALETTE ) +
  120.                colors * sizeof( PALETTEENTRY ) );
  121.  
  122.             // Fill in the logical palette
  123.             lp->palVersion = 0x0300;
  124.             lp->palNumEntries = colors;
  125.  
  126.             // Copy the colors from the bitmap to the palette
  127.             for (int i = 0; i < colors; i++) {
  128.                lp->palPalEntry[ i ].peRed   = bmi->bmiColors[ i ].rgbRed;
  129.                lp->palPalEntry[ i ].peGreen = bmi->bmiColors[ i ].rgbGreen;
  130.                lp->palPalEntry[ i ].peBlue  = bmi->bmiColors[ i ].rgbBlue;
  131.                lp->palPalEntry[ i ].peFlags = 0;
  132.             }
  133.  
  134.             // Create the palette and free the logical palette
  135.             *palette = CreatePalette( lp );
  136.             free( lp );
  137.          }
  138.  
  139.          else
  140.  
  141.             // Set the palette handle to zero for 24-bit bitmaps
  142.             *palette = 0;
  143.  
  144.          // Create the bitmap
  145.          HDC DC = GetDC( 0 );
  146.          *palette = SelectPalette( DC, *palette, FALSE );
  147.          RealizePalette( DC );
  148.          ldib = CreateDIBitmap( DC, bmih, CBM_INIT,
  149.             ((LPSTR)(bmih + 1)) + (colors * sizeof( RGBQUAD )),
  150.             bmi, DIB_RGB_COLORS );
  151.          *palette = SelectPalette( DC, *palette, FALSE );
  152.          ReleaseDC( 0, DC );
  153.       }
  154.    }
  155.  
  156.    // Close the file, release the global memory block, and return
  157.    _lclose( hFile );
  158.    GlobalUnlock( buffer );
  159.    GlobalFree( buffer );
  160.    return ldib;
  161. }
  162.  
  163. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  164.  
  165. HPALETTE EWBMP_OnAskPalette( HWND hWnd ) {
  166.  
  167.    EWBMPINFO *hbi;
  168.  
  169.    // Get the palette from the bitmap info stored with the window
  170.    hbi = (EWBMPINFO*)GetWindowLong( hWnd, 0 );
  171.    return hbi->hpal;
  172. }
  173.  
  174. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  175.  
  176. BOOL EWBMP_OnQuerySize( HWND hWnd, HDC DC, POINT *size ) {
  177.  
  178.    EWBMPINFO *hbi;
  179.  
  180.    // Get the size from the bitmap info stored with the window
  181.    hbi = (EWBMPINFO*)GetWindowLong( hWnd, 0 );
  182.    size->x = hbi->width;
  183.    size->y = hbi->height;
  184.    return TRUE;
  185. }
  186.  
  187. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  188.  
  189. BOOL EWBMP_OnCreate( HWND hWnd, CREATESTRUCT *lpCreateStruct ) {
  190.  
  191.    CREATEINFO *ci;
  192.    EWBMPINFO *hbi;
  193.    BITMAP bmp;
  194.  
  195.    // Add a border to the window
  196.    SetWindowLong( hWnd, GWL_STYLE, GetWindowLong( hWnd, GWL_STYLE ) | WS_BORDER );
  197.  
  198.    // Allocate structure for window bitmap information and save
  199.    hbi = (EWBMPINFO*)malloc( sizeof( EWBMPINFO ) );
  200.    SetWindowLong( hWnd, 0, (LONG)hbi );
  201.  
  202.    // Read the bitmap file
  203.    ci = (CREATEINFO*)lpCreateStruct->lpCreateParams;
  204.    hbi->hbmp = LoadDIBitmap( ci->lpstrAuthorData, &hbi->hpal );
  205.  
  206.    // If a bitmap was read then determine its size
  207.    if (hbi->hbmp != 0) {
  208.       GetObject( hbi->hbmp, sizeof( BITMAP ), &bmp );
  209.       hbi->width = bmp.bmWidth;
  210.       hbi->height = bmp.bmHeight;
  211.    }
  212.    else {
  213.       hbi->width = 0;
  214.       hbi->height = 0;
  215.    }
  216.    return TRUE;
  217. }
  218.  
  219. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  220.  
  221. void EWBMP_OnDestroy( HWND hWnd ) {
  222.  
  223.    EWBMPINFO *hbi;
  224.  
  225.    // Retrieve the struc